package com.anywhere.freightman.order.controller;

import com.anywhere.freightman.common.ContextKeys;
import com.anywhere.freightman.common.annotation.Permission;
import com.anywhere.freightman.common.entity.ReportDayCount;
import com.anywhere.freightman.common.entity.ReturnResp;
import com.anywhere.freightman.common.entity.user.Account;
import com.anywhere.freightman.order.service.IWaybillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("report")
@Api(tags = "报表")
public class ReportController {

    @Autowired
    private IWaybillService waybillService;

    @GetMapping("order")
    @Permission(code = "COMMON")
    @ApiOperation("运单统计")
    public ReturnResp getOrderReport(HttpServletRequest httpRequest,
                                     @RequestParam(value = "startTime", required = false) String startTime,
                                     @RequestParam(value = "endTime", required = false) String endTime,
                                     @RequestParam(value = "shipper", required = false) String shipper,
                                     @RequestParam(value = "receiver", required = false) String receiver) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        List<ReportDayCount> reportCount = waybillService.getReportCount(account.getCompanyId(), startTime, endTime, shipper, receiver);
        List<String> dateArr = waybillService.getContinuousDate(startTime, endTime);

        List<ReportDayCount> dataList = new ArrayList<>();

        Map<String, ReportDayCount> map = reportCount.stream().collect(Collectors.toMap(ReportDayCount::getDay, ReportDayCount::getThis));
        dateArr.forEach(item -> {
            if (map.get(item) != null) {
                dataList.add(map.get(item));
            } else {
                ReportDayCount tmp = new ReportDayCount();
                tmp.setDay(item);
                dataList.add(tmp);
            }
        });


        List<Integer> orderCountList = dataList.stream().map(ReportDayCount::getCount).collect(Collectors.toList());
        List<Double> orderCostList = dataList.stream().map(ReportDayCount::getCost).collect(Collectors.toList());
        List<Double> orderWeightList = dataList.stream().map(ReportDayCount::getWeight).collect(Collectors.toList());


        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("dateArr", dateArr);
        resultMap.put("orderCount", orderCountList);
        resultMap.put("orderCost", orderCostList);
        resultMap.put("orderWeight", orderWeightList);
        return ReturnResp.buildOk(resultMap);
    }
}
